package com.lovepick.database;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.lovepick.bean.Order;
import com.lovepick.util.Constant;

public class OrderDbManager
{
	private final String TAG = "OrderDbManager";
	private DatabaseHelper mDbHelper = null;
	private SQLiteDatabase mDatabase = null;
	private final String mTableName;

	public OrderDbManager(String role)
	{
		if (role.equals(Constant.DRIVER))
		{
			mTableName = Order.ORDER_DRIVER_TABLE;
		}
		else
		{
			mTableName = Order.ORDER_PASSENGER_TABLE;
		}
		mDbHelper = new DatabaseHelper();
	}

	public List<Order> getOrdersAll()
	{
		List<Order> orders = null;
		String sql = "SELECT * FROM " + mTableName + "ORDER BY " + Order.GOTIME;
		boolean isCursorNull = true;
		try
		{
			mDatabase = mDbHelper.getReadableDatabase();
			Log.i(TAG, sql);
			Cursor cursor = mDatabase.rawQuery(sql, null);
			orders = new ArrayList<Order>();
			while (cursor.moveToNext())
			{
				isCursorNull = false;
				Order order = new Order();
				order.setOrderID(cursor.getInt(cursor.getColumnIndex(Order.ORDER_ID)));
				order.setNickName(cursor.getString(cursor.getColumnIndex(Order.NICKNAME)));
				order.setGoTime(cursor.getString(cursor.getColumnIndex(Order.GOTIME)));
				order.setSrc(cursor.getString(cursor.getColumnIndex(Order.SRC)));
				order.setSlat(cursor.getDouble(cursor.getColumnIndex(Order.SLAT)));
				order.setSlng(cursor.getDouble(cursor.getColumnIndex(Order.SLNG)));
				order.setDest(cursor.getString(cursor.getColumnIndex(Order.DEST)));
				order.setDlat(cursor.getDouble(cursor.getColumnIndex(Order.DLAT)));
				order.setDlng(cursor.getDouble(cursor.getColumnIndex(Order.DLNG)));
				order.setMoney(cursor.getInt(cursor.getColumnIndex(Order.MONEY)));
				order.setState(cursor.getString(cursor.getColumnIndex(Order.STATE)));
				order.setOffset(cursor.getInt(cursor.getColumnIndex(Order.OFFSET)));
				order.setCount(cursor.getInt(cursor.getColumnIndex(Order.COUNT)));
				order.setUserID(cursor.getInt(cursor.getColumnIndex(Order.MEM_ID)));

			}
			if (isCursorNull)
			{
				Log.i(TAG, "cursor is null");
				orders = null;
			}
			mDatabase.close();
		}
		catch (Exception e)
		{
			// TODO: handle exception
			mDatabase.close();
			orders = null;
		}

		return orders;
	}

	public void updateOrderState(Order.OrderUpdate update)
	{
		final String sql = "UPDATE " + mTableName
				+ " SET "
				+ Order.STATE + "=?"
				+ " WHERE "
				+ Order.ORDER_ID + "=?;";
		Object[] bindArgs = new Object[]
				{update.stateUp, update.orderIdUp};
		mDatabase = mDbHelper.getWritableDatabase();
		Log.i(TAG, sql);
		mDatabase.execSQL(sql, bindArgs);
		mDatabase.close();
	}

	public void updateOrderState(List<Order.OrderUpdate> updates)
	{
		Log.i(TAG, "---updateOrderState(List)---");
		final String sql = "UPDATE " + mTableName
				+ " SET "
				+ Order.STATE + "=?"
				+ " WHERE "
				+ Order.ORDER_ID + "=?;";
		mDatabase = mDbHelper.getWritableDatabase();
		for (Order.OrderUpdate orderUpdate : updates)
		{
			Object[] bindArgs = new Object[] {orderUpdate.stateUp, orderUpdate.orderIdUp};
			mDatabase.execSQL(sql, bindArgs);
		}
		mDatabase.close();
	}

	public void addOrders(List<Order> orders)
	{
		Log.i(TAG, "---addOrders(List)---");
		final String sql = "INSERT INTO " + mTableName
				+ " ("
				+ Order.ORDER_ID + ", "
				+ Order.NICKNAME + ", "
				+ Order.SRC + ", "
				+ Order.SLAT + ", "
				+ Order.SLNG + ", "
				+ Order.DEST + ", "
				+ Order.DLAT + ", "
				+ Order.DLNG + ", "
				+ Order.GOTIME + ", "
				+ Order.COUNT + ", "
				+ Order.MONEY + ", "
				+ Order.OFFSET + ", "
				+ Order.STATE + ", "
				+ Order.MEM_ID
				+ ")"
				+ " VALUES "
				+ "(?,?,?,?,?,?,?,?,?,?,?,?,?);";

		mDatabase = mDbHelper.getWritableDatabase();
		for (Order order : orders)
		{
			Object[] bindArgs = new Object[] {
				order.getOrderID(),
				order.getNickName(),
				order.getSrc(),
				order.getSlat(),
				order.getSlng(),
				order.getDest(),
				order.getDlat(),
				order.getDlng(),
				order.getGoTime(),
				order.getCount(),
				order.getMoney(),
				order.getOffset(),
				order.getState(),
				order.getUserID()
			};
			mDatabase.execSQL(sql, bindArgs);
		}

		mDatabase.close();
	}

}
